package com.example.testdemo.coordinator;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

/**
 * Created by 颜 on 05.07.007.
 * 自定义下拉刷新View
 */

public class SmartRefreshHeader extends ClassicsHeader implements RefreshHeader {

    private ImageView blurImage;
    private ImageView image;
    private ViewGroup.LayoutParams bottomParams;
    private ImageView bottomImage;
    private int imageHeight;
    private int imageWidth;

    /**
     * 自定义构造器
     * 所有方法均添加 super 来防止父类方法被覆盖从而导致刷新状态失效的问题
     * @param bottomImage 最底层负责拉伸的ImageView
     * @param image       嵌套在ToolBar内的原图
     * @param blurImage  嵌套在ToolBar内的模糊图片
     */
    public SmartRefreshHeader(Context context, ImageView bottomImage, ImageView image, ImageView blurImage) {
        super(context);
        //将刷新字颜色改为白色
        setAccentColor(Color.WHITE);
        this.bottomImage = bottomImage;
        this.image = image;
        this.blurImage = blurImage;
        initBottomParams();
    }

    /**
     * 初始化底部图片 数据 宽高 LayoutParams
     */
    private void initBottomParams() {
        bottomImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                imageHeight = bottomImage.getHeight();
                imageWidth = bottomImage.getWidth();
                bottomParams = bottomImage.getLayoutParams();
                bottomImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    public SmartRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 下拉过程
     * 通过偏移量来重新设定底部ImageView的高度来适应下拉刷新
     * @param offset 偏移量
     */
    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {
        super.onPullingDown(percent, offset, headerHeight, extendHeight);
        bottomParams.height = imageHeight + offset;
        bottomParams.width = imageWidth + offset;
        bottomImage.setLayoutParams(bottomParams);
    }

    /**
     * 回弹过程
     * 通过偏移量来重新设定底部ImageView的高度来适应下拉刷新
     * @param offset 偏移量
     */
    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {
        super.onReleasing(percent, offset, headerHeight, extendHeight);
        bottomParams.height = imageHeight + offset;
        bottomImage.setLayoutParams(bottomParams);
    }

    @NonNull
    @Override
    public View getView() {
        return super.getView();
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return super.getSpinnerStyle();
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
        super.onInitialized(kernel, height, extendHeight);
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
        super.onHorizontalDrag(percentX, offsetX, offsetMax);
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        super.onStartAnimator(layout, height, extendHeight);
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        return super.onFinish(layout, success);
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return super.isSupportHorizontalDrag();
    }

    /**
     * 下拉状态改变
     * 通过下拉刷新的 None 空闲状态来控制原图和模糊图层的显示，以及隐藏背景层
     * 通过 PullDownToRefresh 下拉中的状态来隐藏 原图和模糊图层，显示背景层，配合 onPullingDown 重新设定背景图层高度
     * @param oldState 上一个状态
     * @param newState 最新状态
     */
    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        super.onStateChanged(refreshLayout, oldState, newState);
        if (newState == RefreshState.None) {
            System.out.println("刷新结束了");
            image.setVisibility(View.VISIBLE);
            blurImage.setVisibility(View.VISIBLE);
            bottomImage.setVisibility(GONE);
        }
        if (newState == RefreshState.PullDownToRefresh) {
            bottomImage.setVisibility(VISIBLE);
            image.setVisibility(View.GONE);
            blurImage.setVisibility(View.GONE);
        }
    }
}
